//
//  CZQMyOdeeChildVC.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/20.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQMyOdeeChildVC.h"
#import "CZQOderTableViewCell.h"
#import "CZQOderDetailVC.h"
#import "CZQCourseOderModel.h"
@interface CZQMyOdeeChildVC ()<UITableViewDelegate,UITableViewDataSource>
{
    int startIndex;
    
    int pageSize;
}


@property(nonatomic,strong)NoDataView *noDataView;
/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

@end

@implementation CZQMyOdeeChildVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BackGroundColor;
    
//    [self handleData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [self loadData];
    
}


#pragma mark -- 懒加载

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

#pragma mark 下拉
-(void)loadStartData{
    
    startIndex = 1;
    
    pageSize = 20;
    
    [self loadData];
}

#pragma mark 上拉
-(void)loadMoreData{
    
    startIndex = startIndex + 1;
    
    [self loadData];
}
-(void)loadData{
    
    //Get_GetCourseWareListData
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    
    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"UserID"] forKey:@"TeacherID"];
    
    [params setObject:[NSString stringWithFormat:@"%d",startIndex] forKey:@"page"];
    
    [params setObject:[NSString stringWithFormat:@"%d",pageSize] forKey:@"rows"];
    
    [HTTPTool GET:Get_GetCourseOrderListData parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              NSLog(@"%@",responseObject);
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  if (resultArray.count > 0) {
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshing];
                  }
                  else{
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                  }
                  
                  self.noDataView = [NoDataView shareInstance];
                  
                  [self.noDataView hideInView:self.view];
                  
                  
                  if (startIndex == 1) {
                      
                      self.dataArr = [NSMutableArray arrayWithArray:resultArray];
                      
                      if (self.dataArr.count == 0) {
                          
                          self.noDataView = [NoDataView shareInstance];
                          
                          [self.noDataView showInView:self.view];
                      }
                      
                      
                      
                      
                  }
                  else{
                      
                      [self.dataArr addObjectsFromArray:resultArray];
                      
                      
                  }
                  
                  
                  
                  
                  
                  [self.tableView reloadData];
              }
              
          }
     
          failure:^(NSError *error){
              [self.tableView.mj_header endRefreshing];
              
              [self.tableView.mj_footer endRefreshing];
              [MBProgressHUD hideHUDForView:self.view];
          }];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        tableView.backgroundColor = CSClearColor;
        
        tableView.estimatedRowHeight = 150;
        
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
        }];
        
        //        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册
        //                [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        
        [tableView registerNib:[UINib nibWithNibName:@"CZQOderTableViewCell" bundle:nil]  forCellReuseIdentifier:@"cell"];
        
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        footer.automaticallyHidden = YES;
        
        MyTableHead *mj_head = [MyTableHead headerWithRefreshingTarget:self refreshingAction:@selector(loadStartData)];
        tableView.mj_header = mj_head;
        tableView.mj_footer = footer;
        [tableView.mj_header beginRefreshing];
        [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
        }];
        
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count ;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CZQOderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    CZQCourseOderModel *oder = [CZQCourseOderModel mj_objectWithKeyValues:[self.dataArr objectAtIndex:indexPath.row]] ;
    
    [cell.iconImage sd_setImageWithURL:[Helper imageUrlStringToChinese:oder.Pic imageType:nil] placeholderImage:PlaceHold];
    
    NSString *str = [NSString stringWithFormat:@"%@元",oder.FactFee];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]
                                          initWithString:str];
    
    [attrStr addAttribute:NSFontAttributeName value:
     [UIFont boldSystemFontOfSize:18.0f] range:NSMakeRange(0, str.length -1)];
    
    [attrStr addAttribute:NSFontAttributeName value:
     [UIFont systemFontOfSize:14.0f] range:NSMakeRange(str.length -1, 1)];
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:
     TextColor1 range:NSMakeRange(str.length -1, 1)];
    
    cell.priceLable.attributedText = attrStr;
    
    cell.titleLable.text = [NSString stringWithFormat:@"%@|%@",oder.Name,oder.TypeID];
    
    cell.YXJLable.text = [NSString stringWithFormat:@"意向金：%@",oder.WannaFee];
    
    cell.SFKLable.text = [NSString stringWithFormat:@"实付款：%@",oder.FactFee];
    
    cell.titleLable.text = [NSString stringWithFormat:@"有效期：%@",oder.EndDateTime];
    
    
    return cell  ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CZQOderDetailVC *detail = [CZQOderDetailVC new];
    
    CZQCourseOderModel *oder = [CZQCourseOderModel mj_objectWithKeyValues:[self.dataArr objectAtIndex:indexPath.row]] ;
    
    detail.oder = oder;
    
    [self.navigationController pushViewController:detail animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
